/* NOTICE
    OpenJaWS - Open Java Weather Station
    
    Copyright (C) 2008 Grant Gardner <grant@lastweekend.com.au>
    
    OpenJaWS is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
NOTICE */

package au.com.lastweekend.openjaws.plugins;

import java.util.Date;

import au.com.lastweekend.openjaws.api.Readings;
import au.com.lastweekend.openjaws.api.WeatherPlugin;
import au.com.lastweekend.openjaws.api.WeatherPluginException;

/**
 * Convenience WeatherPlugin superclass that knows how to skip old readings and has enabled/disabled state.
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: codetemplates.xml,v 1.2 2005/01/22 06:53:11 ggardner Exp $
 */
public abstract class AbstractWeatherPlugin implements WeatherPlugin {

    private static final Date MIN_DATE = new Date(0);

    private Date lastReading;
    private boolean enabled = true;

    /**
     * Calls process readings if this plugin is enabled and we aren't skipping an old reading
     * 
     * @param readings
     * @throws WeatherPluginException
     */
    public final void process(Readings readings) throws WeatherPluginException {

        if (isEnabled() && !isOldReading(readings)) {
            processReadings(readings);
        }
    }

    /**
     * 
     * @return the last reading processed by this plugin before this instance was initialised or null if this is not important
     */
    protected Date getLastReadingTime() {

        return null;
    }

    /**
     * @see WeatherPlugin#process(Readings)
     * @param readings
     * @throws WeatherPluginException
     */
    protected abstract void processReadings(Readings readings) throws WeatherPluginException;

    private boolean isOldReading(Readings readings) {

        // If the first reading is a historical reading, then we
        // get the last persisted reading for this plugin,
        // and then we skip any readings before that time.
        if (lastReading == null) {
            if (HistoricalReadingsPlugin.isHistoricalReading(readings)) {
                lastReading = getLastReadingTime();
            }
            // If we are still null, then all readings pass
            if (lastReading == null) {
                lastReading = MIN_DATE;
            }
        }

        return !readings.getReadingTime().after(lastReading);
    }

    public boolean isEnabled() {

        return this.enabled;
    }

    /**
     * 
     * @param enabled
     *            false to disable this plugin
     */
    public void setEnabled(boolean enabled) {

        this.enabled = enabled;
    }

}